package com.zzyy.study.synchroizedSaleTicket;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Description:
 * <p>
 * ClassName: Ticket
 * date: 2021/2/24 14:01
 *
 * @author zzjx
 * @version 1.0
 * @since JDK 1.8
 */
class SaleTicket{
    private int number = 30;
    private ReentrantLock lock = new ReentrantLock(true);
    public  synchronized void sale()
    {
        if(number>0){
            System.out.println(Thread.currentThread().getName()+"\t卖出第: "+(number--)+"\t还剩下: "+number);
        }
    }
}
public class Ticket {
    public static void main(String[] args) {
        SaleTicket ticket = new SaleTicket();
        new Thread(()->{
           for (int i = 1; i <= 33; i++) {
               ticket.sale();
           }
        },"A").start();
        new Thread(()->{
            for (int i = 1; i <= 33; i++) {
                ticket.sale();
            }
        },"B").start();
        new Thread(()->{
            for (int i = 1; i <= 33; i++) {
                ticket.sale();
            }
        },"C").start();
        new Thread(()->{
            for (int i = 1; i <= 33; i++) {
                ticket.sale();
            }
        },"D").start();
    }
}
